package simple3D.core 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Vector3D;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class Container3D extends Sprite
	{
		private var _surfaces:Vector.<Surface3D>;
		private var _rotation3D:Vector3D;
		private var _position3D:Vector3D;
		
		public function Container3D() 
		{
			super();
			_surfaces = new Vector.<Surface3D>();
			_rotation3D = new Vector3D(0,0,0);
			_position3D = new Vector3D(0,0,0);
		}
		
		public function update():void
		{
			//array to store references to shapes
			var shapes:Array = [];
			//get number of objects in the display list
			var len:uint = this.numChildren;
			var surface:Surface3D;
			//loop thru objects and add Shape3D objects to our array
			for (var i:uint=0;i<len;i++)
			{
				if (this.getChildAt(i) is Surface3D)
				{
					shapes.push(this.getChildAt(i));
					
					//rotate
					surface = this.getChildAt(i) as Surface3D;
					//surface.position.add(_position3D);
					//surface.angle.x += _rotation3D.x;
					//surface.angle.y += _rotation3D.y;
					//surface.angle.z += _rotation3D.z;
					surface.update();
				}
				
			}
			//sort the array based on the z position of the objects
			shapes = shapes.sortOn(["distance"], Array.NUMERIC|Array.DESCENDING);
			//move each object in turn to the top (front) of the display list
			len = shapes.length;
			for (i=0;i<len;i++)
				this.addChild(shapes[i]);
		}
		
		override public function addChild(child:DisplayObject):flash.display.DisplayObject 
		{
			return super.addChild(child);
		}
		
		private function sortDepths():void
		{
			//array to store references to shapes
			var shapes:Array = [];
			//get number of objects in the display list
			var len:uint = this.numChildren;
			//loop thru objects and add Shape3D objects to our array
			for (var i:uint=0;i<len;i++)
			{
				if (this.getChildAt(i) is Surface3D)
					shapes.push(this.getChildAt(i));
			}
			//sort the array based on the z position of the objects
			shapes = shapes.sortOn(["distance"], Array.NUMERIC|Array.DESCENDING);
			//move each object in turn to the top (front) of the display list
			len = shapes.length;
			for (i=0;i<len;i++)
				this.addChild(shapes[i]);
		}
		
		public function get rotation3D():Vector3D 
		{
			return _rotation3D;
		}
		
		public function set rotation3D(value:Vector3D):void 
		{
			_rotation3D = value;
		}
		
		public function get position3D():Vector3D 
		{
			return _position3D;
		}
		
		public function set position3D(value:Vector3D):void 
		{
			_position3D = value;
		}
		
		
		
	}

}